package com.Merlin.myapplication

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.Merlin.myapplication.fragment.DetailFragment
import com.Merlin.myapplication.fragment.ListFragment

class MainActivity : AppCompatActivity(), ListFragment.OnItemClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        // 加载Fragment
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container, ListFragment())
                .commit()
        }
    }

    override fun onItemClick(position: Int) {
        // Toast.makeText(this, "点击了第 $position 项", Toast.LENGTH_SHORT).show()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, DetailFragment()) // 替换Fragment
            .addToBackStack(null) // 添加到返回栈
            .commit() // 提交事务
    }
}